<?php
	session_start();
	require_once("db_connect.php");

	$thread_id = htmlspecialchars($_GET["thread_id"],ENT_QUOTES);
	$thread_name = "";
	$thread_date = "";

	$sql = "SELECT thread_name,thread_date FROM thread WHERE thread_id = ?";
	$data = $thread_id;
	$res =& $db->query($sql, $data);

	if (PEAR::isError($res)) {
		die($res->getMessage());
	}

	while ($row =& $res->fetchRow(DB_FETCHMODE_ASSOC)) {
	    $thread_name = $row['thread_name'];
	    $thread_date = $row['thread_date'];
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet"  href="../css/css.css" type="text/css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>
<script type="text/javascript" src="../js/jquery.corner.js"></script>
<script type="text/javascript">
	var isLogin = <?php echo array_key_exists("user_id",$_SESSION)?"true":"false"; ?>

	$(document).ready(
		function() {
			$('.box').corner();
			$('.box2').corner();
			$('.box3').corner();
			$('.box4').corner();
			<?php
					echo array_key_exists("error",$_SESSION)?"login_error('".htmlspecialchars($_SESSION['error'],ENT_QUOTES,"UTF-8")."');":"";
					unset($_SESSION["error"]);
			?>
	});

	function writeIn(){
		if(!isLogin){
			alert('ログインして下さい');
			return false;
		}
	}

	function delete_thread(t_id){
		if(window.confirm('スレッドを削除しますか？')){
			location.href = "./thread_delete.php?&thread_delete_id="+t_id;
		}
		else{
			window.alert('キャンセルされました'); // 警告ダイアログを表示
		}

	}

	function delete_response(r_id,t_id){
		if(window.confirm('書き込みを削除しますか？')){
			location.href = "./response_delete.php?response_id="+r_id+"&thread_id="+t_id;
		}
		else{
			window.alert('キャンセルされました'); // 警告ダイアログを表示
		}
	}
</script>
<title><?php echo $thread_name?></title>
</head>
<body>
	<div id="container">
		<div id="header" class="box4">
			<div id="logo">
			</div>
<div id="header" class="box4">
			<div id="header_box" >
				<div id="title">
				</div>
				<div id="search_box">
					<a href="#" id="volunteer">ボランティア</a>｜<a href="#" id="thread">掲示板</a>
					<form method="post" action="search_function/search_display.php?target=volunteer" id="target">
						<input type="text"  name="search_word"><br />
						<input type="submit" value="検索する" id="search_button">
					</form>
				</div>
			</div>
		</div>
		</div>
		<div id="rightnavi" class="box">
		<?php if(empty($_SESSION["user_name"])&&empty($_SESSION["user_id"])){?>
			<h3>ログインフォーム</h3>
			<form method="post" action="./user_function/UserLogin.php">
			<table>
				<tr><td>ID：</td><td><input type="text" name="id" size="10"></td></tr>
				<tr><td>PASS：</td><td><input type="text" name="pass" size="10"></td></tr>
			</table>
				<p><input type="image" src="../img/login.png" id="login_logo"/></p>
			</form>
			<form action="user_function/User_create.php">
				<p><input type="image" src="../img/newuser.png" id="new_member_logo" /></p>
			</form>
		<?php }else{?>
				<img src="../img/kaiinlogo.png" id="kaiinlogo" />
				<p>ようこそ<?php echo $_SESSION['nick_name']?>さん！</p>
				<p><a href="./user_function/user_infomation.php">会員情報表示</a></p>
				<p><a href="./user_function/user_update.php">会員情報変更</a></p>
				<p><a href="./user_function/user_display_application.php">申請中ボランティア</a></p>
				<p><a href="./user_function/user_display_volunteer.php">提供中ボランティア</a></p>
				<p><a href="./volunteer_function/volunteer_information_insert.php">ボランティア作成</a></p>
				<p><a href="./user_function/logout.php">ログアウト</a></p>
				<br />
		<?php }?>
		<img src=".././img/keizilogo.png" id="keizilogo" />
				<ul class="menu_margin">
	<div id="thread_title_look">
<?php
	//スレッド項目取得
	$sql = "SELECT t.thread_id,count(thread_name) as thread_count,t.thread_name,max(r.response_date) as response_date,t.thread_id FROM thread AS t LEFT OUTER JOIN response AS r ON t.thread_id = r.thread_id group by t.thread_name order by response_date desc limit 0,5";
	$sth = $db->query($sql);
	if (DB::isError($sth)) {
	  exit($sth->getMessage());
	}

	//スレッド表示
	while ($data = $sth->fetchRow(DB_FETCHMODE_ASSOC)) {
	  echo "<li><a href='./response.php?thread_id=".$data['thread_id']."'>";
	  echo mb_strlen($data['thread_name'])>=9?mb_substr($data['thread_name'],0,9)."...":$data['thread_name'];
	  echo "</a></li>";
	}
?>
</div>
				</ul>
				<div id="more"><a href="./thread.php">もっと見る...</a></div>
	</div>
<div id="main"  class="box3">
<?php
	if(!$thread_name){
	//	header("Location: ../index.php");
	}
	$sql = "SELECT  response.thread_id,response.user_id,response.response_id,response.response_data,response.response_date,user.nick_name FROM response,user WHERE response.user_id = user.user_id and thread_id = ?";

	$data = $thread_id;
	$res =& $db->query($sql, $data);

	if (PEAR::isError($res)) {
		die($res->getMessage());
	}
?>

 <div id="leftmenu">
	<div id="lefttitle">
		<h3><?php echo $thread_name ?> <span class="h3sub"><?php echo $thread_date ?></span></h3>
	</div>
	<ul>
<?php
	$i = 1;
	while ($row =& $res->fetchRow(DB_FETCHMODE_ASSOC)) {	//SQLの実行結果を表示
	    echo "<li class='date'><span class='thread_id'>".$i."</span>:".$row['nick_name']."　".$row['response_date'];
	    if((array_key_exists("user_id",$_SESSION))&&($_SESSION["user_id"] == $row['user_id'])){
	    	if($i==1){
	    		echo "　<input type='button' value='スレッド削除' onClick='delete_thread(".$row['thread_id'].")'>";
	    	}else{
	    		echo "　<input type='button' value='レス削除' onClick='delete_response(".$row['response_id'].",".$row['thread_id'].")'>";
	    	}
	    }
	    echo "<br>";
	    echo "<li class='newtxt'>".$row['response_data']."</li>";
	    $i++;
	}
?>
</ul>
</div>
<?php
if(array_key_exists("user_id",$_SESSION)){
	echo '<form action="response_insert.php" method="POST" id="thread_form" onSubmit="return writeIn()">';
	echo '<textarea name="response_data" rows="10" cols="72">';
	echo '</textarea><br>';
	echo '<input type="submit" value="書き込む" id="thread_button" />';
	echo '<input type="hidden" name="thread_id" value="'.$thread_id.'">';
	echo '</form>';
}
?>
<br />
</div>
<div id="space"></div>
</div>
</body>
</html>